package health

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/health"
	healthReq "gitcode.com/G-YT/ANN/server/model/health/request"
	"gitcode.com/G-YT/ANN/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type FdJqApi struct {
}

var fdJqService = service.ServiceGroupApp.HealthServiceGroup.FdJqService

// CreateFdJq 創建节气
// @Tags FdJq
// @Summary 創建节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.FdJq true "創建节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"創建成功"}"
// @Router /fdJq/createFdJq [post]
func (fdJqApi *FdJqApi) CreateFdJq(c *gin.Context) {
	var fdJq health.FdJq
	err := c.ShouldBindJSON(&fdJq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdJqService.CreateFdJq(&fdJq); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeleteFdJq 删除节气
// @Tags FdJq
// @Summary 删除节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.FdJq true "删除节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /fdJq/deleteFdJq [delete]
func (fdJqApi *FdJqApi) DeleteFdJq(c *gin.Context) {
	var fdJq health.FdJq
	err := c.ShouldBindJSON(&fdJq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdJqService.DeleteFdJq(fdJq); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFdJqByIds 批量删除节气
// @Tags FdJq
// @Summary 批量删除节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /fdJq/deleteFdJqByIds [delete]
func (fdJqApi *FdJqApi) DeleteFdJqByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdJqService.DeleteFdJqByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFdJq 更新节气
// @Tags FdJq
// @Summary 更新节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.FdJq true "更新节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /fdJq/updateFdJq [put]
func (fdJqApi *FdJqApi) UpdateFdJq(c *gin.Context) {
	var fdJq health.FdJq
	err := c.ShouldBindJSON(&fdJq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdJqService.UpdateFdJq(fdJq); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindFdJq 用id査詢节气
// @Tags FdJq
// @Summary 用id査詢节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query health.FdJq true "用id査詢节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdJq/findFdJq [get]
func (fdJqApi *FdJqApi) FindFdJq(c *gin.Context) {
	var fdJq health.FdJq
	err := c.ShouldBindQuery(&fdJq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if refdJq, err := fdJqService.GetFdJq(fdJq.ID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"refdJq": refdJq}, c)
	}
}

// GetFdJqList 分页獲取节气列表
// @Tags FdJq
// @Summary 分页獲取节气列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.FdJqSearch true "分页獲取节气列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /fdJq/getFdJqList [get]
func (fdJqApi *FdJqApi) GetFdJqList(c *gin.Context) {
	var pageInfo healthReq.FdJqSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := fdJqService.GetFdJqInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

func (fdJqApi *FdJqApi) SetFdJqGraphic(c *gin.Context) {
	var req healthReq.FdJqArticleReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdJqService.SetFdJqGraphic(req); err != nil {
		global.GVA_LOG.Error("设置失敗!", zap.Error(err))
		response.FailWithMessage("设置失敗", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

func (fdJqApi *FdJqApi) GetFdJqGraphic(c *gin.Context) {
	fd_jq_id := c.Query("fd_jq_id")
	if res, err := fdJqService.GetFdJqGraphic(fd_jq_id); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithData(res, c)
	}
}
